# Process this file with autoconf to produce a configure script
AC_INIT(imbus, 0.0.0, suzhe@tsinghua.org.cn)
AC_CONFIG_SRCDIR([src/imbus.h])

###########################################################
##          Some definitions of Version macros.          ##
###########################################################

IMBUS_MAJOR_VERSION=0
IMBUS_MINOR_VERSION=0
IMBUS_MICRO_VERSION=0

IMBUS_VERSION=$IMBUS_MAJOR_VERSION.$IMBUS_MINOR_VERSION.$IMBUS_MICRO_VERSION

AC_SUBST(IMBUS_MAJOR_VERSION)
AC_SUBST(IMBUS_MINOR_VERSION)
AC_SUBST(IMBUS_MICRO_VERSION)
AC_SUBST(IMBUS_VERSION)

# libtool versioning for IMBUS

# increment if the interface has additions, changes, removals.
IMBUS_CURRENT=0

# increment any time the source changes; set to 0 if you increment CURRENT
IMBUS_REVISION=0

# increment if any interfaces have been added; set to 0
# if any interfaces have been removed. removal has 
# precedence over adding, so set to 0 if both happened.
IMBUS_AGE=0

AC_SUBST(IMBUS_CURRENT)
AC_SUBST(IMBUS_REVISION)
AC_SUBST(IMBUS_AGE)

# Define a string for the earliest version that this release has
# binary compatibility with. This is used for module locations.
#
IMBUS_BINARY_VERSION=1.0.0
AC_SUBST(IMBUS_BINARY_VERSION)

AC_DEFINE_UNQUOTED(IMBUS_BINARY_VERSION, "$IMBUS_BINARY_VERSION", [The binary version of IMBUS library.])
AC_DEFINE_UNQUOTED(IMBUS_VERSION, "$IMBUS_VERSION", [The release version of IMBUS library.])
AC_DEFINE_UNQUOTED(IMBUS_MAJOR_VERSION, $IMBUS_MAJOR_VERSION, [The Major version of IMBUS library.])
AC_DEFINE_UNQUOTED(IMBUS_MINOR_VERSION, $IMBUS_MINOR_VERSION, [The Minor version of IMBUS library.])
AC_DEFINE_UNQUOTED(IMBUS_MICRO_VERSION, $IMBUS_MICRO_VERSION, [The Micro version of IMBUS library.])

###########################################################
##                  Start Configuration.                 ##
###########################################################

# Init automake stuff
AM_INIT_AUTOMAKE
AM_CONFIG_HEADER(config.h)

## must come before we use the $USE_MAINTAINER_MODE variable later
AM_MAINTAINER_MODE

# Init libtool
AC_LIBLTDL_INSTALLABLE
AC_LIBTOOL_DLOPEN
AC_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)
AC_SUBST(LIBLTDL)
AC_SUBST(LTDLINCL)
AC_CONFIG_SUBDIRS([libltdl])
AM_CONDITIONAL(IMBUS_BUILD_LIBLTDL, [test x$enable_ltdl_install = xyes])

# libtool option to control which symbols are exported
# right now, symbols starting with _ are not exported
# !!! DO NOT USE IT !!!
LIBTOOL_EXPORT_OPTIONS=''
AC_SUBST(LIBTOOL_EXPORT_OPTIONS)

# Checks for programs.
AC_PROG_CC
AC_LANG(C)

AM_PROG_CC_C_O

# Checks if doxygen is avaiable

# Checks for libraries.
AC_HEADER_STDC


# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T

if test x$ac_cv_type_size_t != xyes; then
  AC_MSG_ERROR([No type size_t, but IMBUS needs it!])
fi

AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short int)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long int)
AC_CHECK_SIZEOF(long long int)
AC_CHECK_SIZEOF(size_t)
AC_CHECK_SIZEOF(double)

AC_CHECK_HEADERS([stdint.h],[has_stdint=yes],[has_stdint=no])

if test x$ac_cv_sizeof_short_int=x2 -a \
	x$ac_cv_sizeof_char=x1 -a \
	x$ac_cv_sizeof_double=x8; then
  :
else
  AC_MSG_ERROR([
*** IMBUS requires
***      sizeof (double)    == 8
***      sizeof (short int) == 2
***      sizeof (char)      == 1
*** You might want to consider using the GNU C compiler.
])
fi

if test x$has_stdint = xyes; then
  INCLUDE_STDINT="#include <stdint.h>"
  INT8="int8_t"
  INT16="int16_t"
  INT32="int32_t"
  INT64="int64_t"
  UINT8="uint8_t"
  UINT16="uint16_t"
  UINT32="uint32_t"
  UINT64="uint64_t"
else
  INCLUDE_STDINT="//#include <stdint.h>"
  INT8="char"
  UINT8="unsigned char"
  INT16="short int"
  UINT16="unsigned short int"
  if test x$ac_cv_sizeof_int = x4; then
    INT32="int"
    UINT32="unsigned int"
  else
    if test x$ac_cv_sizeof_long_int = x4; then
      INT32="long int"
      UINT32="unsigned long int"
    else
      AC_MSG_ERROR([*** No suitable integer type for int32/uint32 found.])
    fi
  fi
  if test x$ac_cv_sizeof_long_long_int = x8; then
    INT64="long long int"
    UINT64="unsigned long long int"
  else
    AC_MSG_ERROR([*** No suitable integer type for int64/uint64 found.])
  fi
fi
AC_SUBST(INCLUDE_STDINT)
AC_SUBST(INT8)
AC_SUBST(INT16)
AC_SUBST(INT32)
AC_SUBST(INT64)
AC_SUBST(UINT8)
AC_SUBST(UINT16)
AC_SUBST(UINT32)
AC_SUBST(UINT64)

dnl check for flavours of varargs macros (test from GLib)
AC_MSG_CHECKING(for ISO C99 varargs macros in C)
AC_TRY_COMPILE([],[
int a(int p1, int p2, int p3);
#define call_a(...) a(1,__VA_ARGS__)
call_a(2,3);
],im_have_iso_c_varargs=yes,im_have_iso_c_varargs=no)
AC_MSG_RESULT($im_have_iso_c_varargs)

AC_MSG_CHECKING(for GNUC varargs macros)
AC_TRY_COMPILE([],[
int a(int p1, int p2, int p3);
#define call_a(params...) a(1,params)
call_a(2,3);
],im_have_gnuc_varargs=yes,im_have_gnuc_varargs=no)
AC_MSG_RESULT($im_have_gnuc_varargs)

dnl Output varargs tests
if test x$im_have_iso_c_varargs = xyes; then
    AC_DEFINE(HAVE_ISO_VARARGS,1,[Have ISO C99 varargs macros])
fi
if test x$im_have_gnuc_varargs = xyes; then
    AC_DEFINE(HAVE_GNUC_VARARGS,1,[Have GNU-style varargs macros])
fi

# check for bytesex stuff
AC_C_BIGENDIAN
case xyes in
x$ac_cv_c_bigendian)
  IM_BYTE_ORDER=IM_BIG_ENDIAN
  IM_ENDIAN_NATIVE=BE
  IM_ENDIAN_ALIEN=LE
  ;;
*)
  IM_BYTE_ORDER=IM_LITTLE_ENDIAN
  IM_ENDIAN_NATIVE=LE
  IM_ENDIAN_ALIEN=BE
  ;;
esac
AC_SUBST(IM_BYTE_ORDER)
AC_SUBST(IM_ENDIAN_NATIVE)
AC_SUBST(IM_ENDIAN_ALIEN)

# Check for check unit test library. 
PKG_CHECK_MODULES([CHECK],[check >= 0.9.3],have_check_lib=yes,have_check_lib=no)

# Check for functions.
AC_CHECK_FUNCS([gethostbyname gethostbyname_r socket bind accept connect listen])

###########################################################
##                      Config options.                  ##
###########################################################
AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug],[enable debug]),enable_debug=$enableval,enable_debug=$USE_MAINTAINER_MODE)
AC_ARG_ENABLE(verbose-mode, AS_HELP_STRING([--enable-verbose-mode],[support verbose debug mode]),enable_verbose_mode=$enableval,enable_verbose_mode=$USE_MAINTAINER_MODE)
AC_ARG_ENABLE(asserts, AS_HELP_STRING([--enable-asserts],[include assertion checks]),enable_asserts=$enableval,enable_asserts=$USE_MAINTAINER_MODE)
AC_ARG_ENABLE(checks, AS_HELP_STRING([--enable-checks],[include sanity checks on public API]),enable_checks=$enableval,enable_checks=yes)
AC_ARG_ENABLE(unit-tests, AS_HELP_STRING([--enable-unit-tests],[build unit tests]),enable_unit_tests=$enableval,enable_unit_tests=$USE_MAINTAINER_MODE)

if test x$enable_debug = xyes; then
    AC_DEFINE(IM_ENABLE_DEBUG,1,[Enable debug])
    CFLAGS="-g -O0 -Werror"
fi

if test x$enable_verbose_mode = xyes; then
    AC_DEFINE(IM_ENABLE_VERBOSE_MODE,1,[Support a verbose mode])
fi

if test x$enable_asserts = xno; then
    AC_DEFINE(IM_DISABLE_ASSERT,1,[Disable assertion checking])
fi

if test x$enable_checks = xno; then
    AC_DEFINE(IM_DISABLE_CHECKS,1,[Disable public API sanity checking])
fi

if test x$enable_unit_tests = xyes -a x$have_check_lib = xyes; then
    build_unit_tests=yes
else
    build_unit_tests=no
fi

AM_CONDITIONAL(IMBUS_BUILD_UNIT_TESTS, [test x$build_unit_tests = xyes])

###########################################################
##                      Output files.                    ##
###########################################################
AC_SUBST(ac_aux_dir)
AC_CONFIG_FILES([Makefile
		 src/Makefile
		 src/imbus_config.h
		 tests/Makefile
		 utils/Makefile
		 unittests/Makefile
		 imbus-1.0.pc])
AC_OUTPUT

AC_MSG_RESULT([
Build options:
  Version                  $IMBUS_VERSION
  Install prefix           $prefix
  Install included libltdl $enable_ltdl_install
  Build shared libs        $enable_shared
  Build static libs        $enable_static
  Build unit tests         $build_unit_tests
  Enable debug             $enable_debug
  Enable verbose mode      $enable_verbose_mode
  Enable asserts           $enable_asserts
  Enable checks            $enable_checks
])

